<template>
  <div class="table-box">
    <ProTable
      ref="proTableRef"
      title="标签表"
      :indent="20"
      :columns="columns"
      :search-columns="searchColumns"
      :request-api="getTableList"
      row-key="id"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary"
          v-auth="'t.tag.create'"
          :icon="CirclePlus"
          @click="openAddEdit('新增标签表')"
        >
          新增
        </el-button>
        <el-button
          v-auth="'t.tag.remove'"
          type="danger"
          :icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
        <el-button
          v-auth="'t.tag.import'"
          type="primary"
          :icon="Upload"
          plain
          @click="importData"
        >
          导入
        </el-button>
        <el-button
          v-auth="'t.tag.export'"
          type="primary"
          :icon="Download"
          plain
          @click="downloadFile"
        >
          导出
        </el-button>
      </template>
      <template #operation="{ row }">
        <el-button
          v-auth="'t.tag.update'"
          type="primary"
          link
          :icon="EditPen"
          @click="openAddEdit('编辑标签表', row, false)"
        >
          编辑
        </el-button>
        <el-button
            v-auth="'t.tag.remove'"
          type="primary"
          link
          :icon="Delete"
          @click="deleteInfo(row)"
        >
          删除
        </el-button>
      </template>
    </ProTable>
    <TTagForm ref="tTagRef" />
    <ImportExcel ref="ImportExcelRef" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import {
  CirclePlus,
  Delete,
  EditPen,
  Upload,
  Download,
} from '@element-plus/icons-vue'
import ProTable from '@/components/ProTable/index.vue'
import {
  createTTagApi,
  removeTTagApi,
  updateTTagApi,
  getTTagListApi,
  getTTagDetailApi,
  importTTagExcelApi,
  exportTTagExcelApi,
} from '@/api/modules/ttag/tTag';
import { useHandleData } from '@/hooks/useHandleData';
import TTagForm from '@/views/article/tTag/components/TTagForm.vue';
import type { ColumnProps, ProTableInstance, SearchProps } from '@/components/ProTable/interface';
import type { ITTag } from '@/api/interface/ttag/tTag';
import ImportExcel from '@/components/ImportExcel/index.vue';
import { downloadTemplate } from '@/api/modules/system/common';
import { ElMessageBox } from "element-plus";
import { useDownload } from "@/hooks/useDownload";
defineOptions({
  name: 'TTagView'
})
const proTableRef = ref<ProTableInstance>();
// 表格配置项
const columns: ColumnProps<ITTag.Row>[] = [
  { type: 'selection', width: 80 },
  { prop: 'tagName', label: '标签名' },
  { prop: 'operation', label: '操作', width: 250, fixed: 'right' }
]
// 搜索条件项
const searchColumns: SearchProps[] = [
  { prop: 'tagName', label: '标签名', el: 'input' },
]
// 获取table列表
const getTableList = (params: ITTag.Query) => {
  let newParams = formatParams(params);
  return getTTagListApi(newParams);
};
const formatParams = (params: ITTag.Query) =>{
  let newParams = JSON.parse(JSON.stringify(params));
  return newParams;
}
// 打开 drawer(新增、查看、编辑)
const tTagRef = ref<InstanceType<typeof TTagForm>>()
const openAddEdit = async(title: string, row: any = {}, isAdd = true) => {
  if (!isAdd) {
    const record = await getTTagDetailApi({ id: row?.id })
    row = record?.data
  }
  const params = {
    title,
    row: { ...row },
    api: isAdd ? createTTagApi : updateTTagApi,
    getTableList: proTableRef.value?.getTableList
  }
  tTagRef.value?.acceptParams(params)
}
// 删除信息
const deleteInfo = async (params: ITTag.Row) => {
  await useHandleData(
    removeTTagApi,
    { ids: [params.id] },
    `删除【${params.id}】标签表`
  )
  proTableRef.value?.getTableList()
}
// 批量删除信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(removeTTagApi, { ids }, '删除所选标签表')
  proTableRef.value?.clearSelection()
  proTableRef.value?.getTableList()
}
// 导入
const ImportExcelRef = ref<InstanceType<typeof ImportExcel>>()
const importData = () => {
  const params = {
    title: '标签表',
    templateName: '标签表',
    tempApi: downloadTemplate,
    importApi: importTTagExcelApi,
    getTableList: proTableRef.value?.getTableList
  }
  ImportExcelRef.value?.acceptParams(params)
}
// 导出
const downloadFile = async () => {
  let newParams = formatParams(proTableRef.value?.searchParam as ITTag.Query);
  useDownload(exportTTagExcelApi, "标签表", newParams);
};
</script>